package com.scorekeeper.utils;

import java.util.ArrayList;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.scorekeeper.R;
import com.scorekeeper.objects.Extension;
import com.scorekeeper.objects.Team;

public class TeamListAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	
	private int mSelectedPos;
	
	//private ArrayList<String> mSeparators = new ArrayList<String>();
	private ArrayList<Team> mTeams = new ArrayList<Team>();
	
	public TeamListAdapter(Context context, int itemResId, Map<Long, Team> teams) {
		super();
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mTeams = new ArrayList<Team>(teams.values());
	}
	
	public void setSelectedPosition(int position){
		mSelectedPos = position;
		// inform the view of this change
		notifyDataSetChanged();
	}
	
	public int getSelectedPosition(){
		return mSelectedPos;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		
		if (v == null) {
			v = inflater.inflate(R.layout.standing_item, null) ;
		} 
		
		//get the team object from the arraylist
		Team team = null;
		
		team = mTeams.get(position);
		
		if (team != null) {
			setTextViewText((TextView) v.findViewById(R.id.standings_pos), Integer.toString(team.standingsPos));
			
			if (team.extensions.get(Extension.ExtensionConstants.CLINCH) != null) {
				setTeamNameText((TextView) v.findViewById(R.id.standings_team), team.name, team.extensions.get(Extension.ExtensionConstants.CLINCH).value);
			} else {
				setTextViewText((TextView) v.findViewById(R.id.standings_team), team.name);
			}
			
			setTextViewText((TextView) v.findViewById(R.id.standings_gp), Integer.toString(team.gamesPlayed));
			setTextViewText((TextView) v.findViewById(R.id.standings_wins), Integer.toString(team.wins));
			setTextViewText((TextView) v.findViewById(R.id.standings_losses), Integer.toString(team.losses));
			setTextViewText((TextView) v.findViewById(R.id.standings_points), Integer.toString(team.points));
			setTextViewText((TextView) v.findViewById(R.id.standings_row), team.extensions.get(Extension.ExtensionConstants.ROW).value);
			setTextViewText((TextView) v.findViewById(R.id.standings_gf), team.extensions.get(Extension.ExtensionConstants.GF).value);
			setTextViewText((TextView) v.findViewById(R.id.standings_ga), team.extensions.get(Extension.ExtensionConstants.GA).value);
			setTextViewText((TextView) v.findViewById(R.id.standings_home), team.extensions.get(Extension.ExtensionConstants.HOME).value);
			setTextViewText((TextView) v.findViewById(R.id.standings_away), team.extensions.get(Extension.ExtensionConstants.AWAY).value);
			setTextViewText((TextView) v.findViewById(R.id.standings_so), team.extensions.get(Extension.ExtensionConstants.SO).value);
			setTextViewText((TextView) v.findViewById(R.id.standings_lastten), team.extensions.get(Extension.ExtensionConstants.LASTTEN).value);
			setTextViewText((TextView) v.findViewById(R.id.standings_streak), team.extensions.get(Extension.ExtensionConstants.STREAK).value);
		}
			
		//redraw
		parent.refreshDrawableState();
		
		return v;
	}
	
	
	private void setTeamNameText(TextView tv, String teamName, String clinched) {
		if (clinched.contentEquals("x")) {
			setTextViewText(tv, "X - " + teamName, Color.GREEN);
		} else if (clinched.contentEquals("y")) {
			setTextViewText(tv, "Y - " + teamName, Color.YELLOW);
		} else if (clinched.contentEquals("p")) {
			setTextViewText(tv, "P - " + teamName, Color.BLUE);
		} else if (clinched.contentEquals("z")) {
			setTextViewText(tv, "Z - " + teamName, Color.RED);
		} else if (clinched.contentEquals("*")){
			setTextViewText(tv, "* - " + teamName, Color.MAGENTA);
		}
	}
	
	private void setTextViewText(TextView tv, String text, int color) {
		if (tv != null) {
			tv.setText(text);
			tv.setTextColor(color);
		}
	}
	
	private void setTextViewText(TextView tv, String text) {
		if (tv != null) {
			setTextViewText(tv, text, Color.GRAY);
		}
	}

	public int getCount() {
		return mTeams.size();
	}

	public Object getItem(int position) {
		return mTeams.get(position);
	}
	
	public long getItemId(int position) {
		return mTeams.get(position)._id;
	}
}